<?php

class Api_ProfileController extends Zend_Rest_Controller
{

	private $db;
	
	
		

   public function init()
    {
        $this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	// Hole den standardmäßigen DB Adapter (definiert in der Boootstrap.ini)
		$this->db = Zend_Db_Table::getDefaultAdapter();
    }
    
    private function authenticate() {
    	// Diese Funktion muss prüfen ob Profile die angezeigt werden sollen auch wirklich zum angegebenen Nutzer gehören.
    	return;
    }

    /**
     * Diese Funktion gibt alle Profile zu einem bestimmten Provider zurück.
     */
	public function indexAction()
    {
    	
    	// Datenbankabfrage zur Auflistung alle Profile des Providers
    	
    	$providerID = $this->getRequest()->getParam('id');
    	
    	$select = new Zend_Db_Select($this->db);
    	$select->from(array('sp'=>'ServiceProvider'))
			->join(array('shp'=>'ServiceProvider_has_ServiceProfile'), 'sp.ServiceProvider_ID = shp.ServiceProvider_ID')
			->join(array('p'=>'ServiceProfile'), 'shp.ServiceProfile_ID = p.ServiceProfile_ID' )
			->join(array('st'=>'ServiceType'), 'p.fk_ServiceType = st.ServiceTypes_ID')
			->where('sp.ServiceProvider_ID =' .'"' . $providerID. '"' );
			
		$profileSet = $this->db->fetchAll($select);
		
		// XML AUFBAUEN
		$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		$xml = $xml . "<ProfileSet userid=\"$providerID\" >\n";
		
		foreach ($profileSet as $key => $value) {
			
			$profileID = $value['ServiceProfile_ID'];
			$xml = $xml . "<Profile id=\"$profileID\" >\n";
			
			$xml = $xml . "<ServiceProfile_name>". $value['ServiceProfile_name'] . "</ServiceProfile_name>\n";
			$xml = $xml . "<ServiceProfile_description>" . $value['ServiceProfile_description'] . "</ServiceProfile_description>\n";
			
			$xml = $xml . "</Profile>\n\n";
		}
		
		$xml = $xml . "</ProfileSet>\n";
		
		
		// RÜCKGABE
		$response = new SimpleXMLElement($xml);
    	
    	
         $this->getResponse()
         	->setHttpResponseCode(200)
            ->appendBody($response->asXML());
    }

    public function getAction()
    {
    	$this->authenticate();
    	
    	$profileid = $this->getRequest()->getParam('profileID');
    	
    	$select = new Zend_Db_Select($this->db);
    	$select->from(array('sp'=>'ServiceProfile'))
			->join(array('st'=>'ServiceType'), 'sp.fk_ServiceType = st.ServiceTypes_ID')
			->where('sp.ServiceProfile_ID =' .'"' . $profileid. '"' );
    	
    	$profile = $this->db->fetchRow($select);
    	
    	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    	
    	$id = $profile['ServiceProfile_ID'];
    	$xml = $xml . "<Profile id=\"$id\" >\n";
    	
    	$name = $profile['ServiceProfile_name'];
    	$xml = $xml . "<ServiceProfile_name>".$name."</ServiceProfile_name>\n";
    	
    	$description = $profile['ServiceProfile_description'];
    	$xml = $xml . "<ServiceProfile_description>".$description."</ServiceProfile_description>\n";
    	
    	$servicetype = $profile['serviceBeschreibung'];
    	$xml = $xml . "<ServiceType>".$servicetype."</ServiceType>\n";
    	
    	$offerid = $profile['fk_Offers'];
    	
    	$xml = $xml . "<OfferID>".$offerid."</OfferID>\n";
    	$xml = $xml . "</Profile>";
    	
   	
    	$response = new SimpleXMLElement($xml);
    	
        $this->getResponse()
        	
        	->setHttpResponseCode(200)
            ->appendBody($response->asXML());
            

    }
    
    public function postAction()
    {
        $this->getResponse()
            ->appendBody("From postAction() creating the requested article");

    }
    
    public function putAction()
    {
        $this->getResponse()
            ->appendBody("From putAction() updating the requested article");

    }
    
    public function deleteAction()
    {
        $this->getResponse()
            ->appendBody("From deleteAction() deleting the requested article");

    }


}
